package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.mapper.IRoleMapper;
import com.zhang.mapper.IUserMapper;
import com.zhang.pojo.LogBean;
import com.zhang.pojo.PowerBean;
import com.zhang.pojo.RoleBean;
import com.zhang.pojo.UserBean;
import com.zhang.service.ILogService;
import com.zhang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private ILogService logService;

    @Override
    public UserBean login(String username, String pwd) {

        UserBean userBean = userMapper.login(username, pwd);
        if (userBean != null){
            List<RoleBean> roles = userBean.getRoleBeans();
            for (RoleBean roleBean : roles) {
                int id = roleBean.getId();
                RoleBean byId = roleMapper.findById(id);
                roleBean.setPowerBeans(byId.getPowerBeans());
            }
            LocalTime now = LocalTime.now();
            String substring = now.toString().substring(0,now.toString().lastIndexOf(":"));
            String content = "用户"+userBean.getUsername()+"在"+substring+"登录系统";
            logService.add(new LogBean(content));
        }
        return userBean;
    }

    @Override
    public IPage<UserBean> findByPage(Page<UserBean> page, String name) {
        IPage<UserBean> byPage = userMapper.findByPage(page, name);
        List<UserBean> userBeanList = new ArrayList<>();
        for (UserBean record : byPage.getRecords()) {
            int id = record.getId();
            List<RoleBean> byUserId = roleMapper.findByUserId(id);
            record.setRoleBeans(byUserId);
            userBeanList.add(record);
        }
        return byPage.setRecords(userBeanList);
    }

    @Override
    public void add(UserBean userBean,Integer[] roleIds) {
        userMapper.addUser(userBean);
        userMapper.addRole(userBean.getId(),roleIds);
    }

    @Override
    public UserBean findById(int userId) {
        UserBean userBean = userMapper.findById(userId);
        List<RoleBean> roleBeans = userBean.getRoleBeans();
        for (RoleBean roleBean : roleBeans) {
            int id = roleBean.getId();
            RoleBean byId = roleMapper.findById(id);
            roleBean.setPowerBeans(byId.getPowerBeans());
        }
        return userBean;
    }

    @Override
    public void del(int userId) {
        userMapper.del(userId);
    }

    @Override
    public void update(int userId,int[] roleIds) {
        userMapper.update(userId, roleIds);
    }

    @Override
    public void updatePwd(int userId, String pwd) {
        userMapper.updatePwd(userId, pwd);
    }

//    public static void main(String[] args) {
//        UserServiceImpl userService = new UserServiceImpl();
//        userService.findByPage(new Page<>(1,2),null);
//    }
}
